<template>
	<view class="company-content">
		<yk-header :title="title"></yk-header>
		<view class="company-search-box">
			<!-- <image class="search-black-btn" src="../../static/img/zhanye/fanhui.png" mode="" @tap="goBack($event)"></image> -->
			<view class="search-input">
				<image class="search-search-btn" src="../../static/img/zhanye/sousuo-hui.png" mode=""></image>
				<input class="search-text" type="text" v-model="companyName"  @input='inputCompanyName($event)' placeholder="请输入公司名称"/> 
			</view>
			<view class="search-no" @click="deleteName()">取消</view>
		</view>
		
		<view class="company-list"> 
			<view 
				v-for="(company, index) in companyList1"
				:key="index"
				:data-id="company.id"
				:data-name="company.name"
				@click='gobind($event)'
			>{{company.name}}</view>
			<!-- <view data-id='1' @tap='gobind($event)'>公司</view> -->
		</view>
		
		<view class="hint" v-if="noSearch">暂未录入你所搜索的公司</view>
	</view>
</template>

<script>
	let page = 1,
		limit = 10;
	export default {
		onLoad(event) {
			page = 1;
			this.getCompanyList();
		},
		onShow() {
		},
		data() {
			return {
				companyList: [],
				companyList1:[],
				List: [],
				loadingText: '加载中...',
				companyName:'',
				noSearch: false,
				title:'绑定礼品卡'
			};
		},
		onReachBottom: function() {
			this.getCompanyList();
		},
		onPullDownRefresh: function() {
			page = 1;
			limit = 10;
			this.companyList = [];
			this.companyList1=[];
			this.List=[];
			this.loadingText = '加载中...';
			this.getCompanyList();
		},
		methods: {
			//顶部功能键
			headBtn(e){
				let that=this;
				uni.showActionSheet({
					itemList: ['刷新', '清除缓存'],
					success: function (res) {
						console.log('选中了第' + (res.tapIndex + 1) + '个按钮');
						if(res.tapIndex==0){
							//刷新
							page = 1;
							limit = 10;
							that.companyList = [];
							that.companyList1=[];
							that.List=[];
							that.loadingText = '加载中...';
							that.getCompanyList();
						}else{
							//清楚缓存
							// callNative('onClearH5Cache')
							uni.showToast({
								title: '清除缓存成功'
							});
						}
					},
					fail: function (res) {
						console.log(res.errMsg);
					}
				});
			},
			//关闭当前页面
			closePage(){
				callNative('navigateBack') 
			},
			//取消按钮
			deleteName(){
				this.companyName='';
				page = 1;
				limit = 10;
				this.companyList = [];
				this.companyList1=[];
			},
			//返回
			goBack(){
				h5NavigateBack ({
					delta: 1
				});
			},
			//返回前一页，将所选公司传回
			gobind(e){
				console.log(e.currentTarget.dataset.id);
				var pages=getCurrentPages(), prevPage=null;
				console.log(pages);
				   if(pages.length>1){
				       prevPage = pages[pages.length - 2];  //上一个页面
				   }
				   if(prevPage){
						console.log(prevPage._data)
				    	prevPage._data.companyId=e.currentTarget.dataset.id;
						prevPage._data.companyName=e.currentTarget.dataset.name;
				   }
				h5NavigateBack ({
					delta: 1
				});
				// uni.redirectTo({
				// 	url: 'bindgift?id='+e.currentTarget.dataset.id+'&name='+e.currentTarget.dataset.name
				// });
			},
			//获取公司列表
			getCompanyList() {
				try {
					// console.log('getbindcardList === ' + uni.getStorageSync('token'));
					uni.request({
						url: this.$usereq + '/coupon-expand/promote/bind/all/company',
						method: 'GET',
						header: {
							'blade-auth': decodeURI(uni.getStorageSync('token')),
							'content-type': 'application/json'
						},
						success: res => {
							let datas = res.data;
							console.log(datas);
							let newList = datas.data;
							if (newList != null && newList != '' && newList.length > 0) {
								this.companyList = this.companyList.concat(newList);
								this.List = this.companyList;
								page++;
								if (newList.length <= 9) {
									this.loadingText = '没有更多了';
								}
							} else {
								this.loadingText = '没有更多了';
							}
							uni.stopPullDownRefresh();
						}
					});
				} catch (e) {
					uni.hideLoading();
					this.loadingText = '没有更多了';
					console.log(e.message);
				}
			},
			//确认选择公司
			goChange(e){
				h5NavigateTo({
					url: 'companyChange'
				});
			},
			//检索所有公司
			inputCompanyName(event){
				// event.detail = {value: value};
				console.log(event.detail);
				this.companyList=this.List
				// this.companyName=event.detail.value;
				var str=event.detail.value;
				if(str==''){
					this.companyList1=[];
					this.noSearch=false;
					return
				}
				var arr=[];
				// console.log(this.companyList);
				for(var i=0;i<this.companyList.length;i++){
					if(this.companyList[i].name.indexOf(str)!=-1){
						arr.push(this.companyList[i]);
					}
				}
				if(arr.length<=0){
					this.noSearch=true
				}
				// console.log(arr);
				this.companyList1=arr;
			}
		}
	};
</script>

<style lang="scss">
	page{
		font-size: 28px;
	}
	.company-search-box{
		width: 100%;
		min-width: 750px;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items:center ;
		padding: 0 20px;
		box-sizing: border-box;
		height: 120px;
		border-bottom: 1px solid #f3f3f3;
		.search-black-btn{
			width: 21px;
			height: 37px;
		}
		.search-input{
			height: 76px;
			width: 630px;
			padding: 0 40px;
			box-sizing: border-box;
			border-radius: 38px;
			display: flex;
			align-items:center;
			background:#E5E5E5;
			.search-search-btn{
				width: 26px;
				height: 26px;
				margin-right: 20px;
			}
			.search-text{
				width: 350px;
			}
		}
		.search-no{
			color: #0096FF;
		}
	}
	.company-list{
		view{
			line-height: 100px;
			border-bottom: 1px solid #f3f3f3;
			padding: 0 20px;
			box-sizing: border-box;
			color: #181818;
		}
	}
	.hint{
		text-align: center;
		margin-top: 80px;
		color: #999;
	}
</style>
